#ifndef __COMM_HPP__
#define __COMM_HPP__

#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include <unistd.h>

using namespace std;

const int size=4097;
const string pathname="/home/wu1/study_-linux";
const int proj_id=0x6666;

key_t GetKey()
{
    key_t k=ftok(pathname.c_str(),proj_id);
    if(k<0)
    {
        perror("ftok fail");
        exit(1);
    }
    printf("ftok success,key is:%d",k);
    return k;
}

int GetShareMemHelper(int flag)
{
    key_t k = GetKey();
    int shmid=shmget(k,size,flag);
    if(shmid<0)
    {
        perror("shmget fail");
        exit(2);
    }
    printf("create share memory success,shmid:%d",shmid);
    return shmid;
}

int CreateShm()
{
    return GetShareMemHelper(IPC_CREAT|IPC_EXCL|0666);
}

int GetShm()
{
    return GetShareMemHelper(IPC_CREAT);
}

#endif